#include "historydao.h"
#include <QSqlRecord>

HistoryDao::HistoryDao(QObject *parent)
    : BaseDao<HistoryItemInfo>(parent)
{
}

HistoryDao::BaseDao::PrimaryKeyStrategy HistoryDao::primaryKeyStrategy() const {
    return BaseDao<HistoryItemInfo>::AutoIncrement;
}

QString HistoryDao::tableName() const {
    return "file_info";
}

QString HistoryDao::primaryKey() const {
    return "file_id";
}

HistoryItemInfo HistoryDao::mapToEntity(const QVariantMap &row) const {
    HistoryItemInfo info;

    info.fileId    = row.value("file_id").toLongLong();
    info.userId    = row.value("user_id").toLongLong();
    info.storagePath    = row.value("storage_path").toString();
    info.uploadTime    = QDateTime::fromString(row.value("upload_time").toString(),  "yyyy-MM-dd HH:mm:ss");
    info.logicalName    = row.value("logical_name").toString();
    info.processType    = row.value("process_type").toString();
    info.fileSize    = row.value("file_size").toLongLong();
    info.fileHash    = row.value("file_hash").toString().toUtf8().constData();  // 添加fileHash字段处理
    info.isUploaded = row.value("is_uploaded").toBool();

    return info;
}

QVariantMap HistoryDao::entityToMap(const HistoryItemInfo &entity) const {
    QVariantMap map;

    if (primaryKeyStrategy() != AutoIncrement || entity.fileId  > 0) {
        map["file_id"] = entity.fileId;
    }

    map["user_id"] = entity.userId;
    map["storage_path"] = entity.storagePath;
    map["upload_time"] = entity.uploadTime.toString("yyyy-MM-dd  HH:mm:ss");
    map["logical_name"] = entity.logicalName;
    map["process_type"] = entity.processType;
    map["file_size"] = entity.fileSize;
    map["file_hash"] = entity.fileHash;  // 添加fileHash字段处理
    map["is_uploaded"] = entity.isUploaded;

    return map;
}

QList<HistoryItemInfo> HistoryDao::findByUserId(qint64 userId, int limit, int offset) {
    QVariantMap params;
    params["user_id"] = userId;

    return paginate("user_id = :user_id",
                    params,
                    "upload_time DESC", //order by
                    limit,
                    offset);
}

QList<HistoryItemInfo> HistoryDao::findByUserIdAndType(qint64 userId, const QString &processType, int limit, int offset) {
    QVariantMap params;
    params["user_id"] = userId;
    params["process_type"] = processType;

    return paginate("user_id = :user_id AND process_type = :process_type",
                    params,
                    "upload_time DESC",
                    limit,
                    offset);
}

int HistoryDao::countByUserId(qint64 userId) {
    QString sql = QString("SELECT COUNT(*) AS count FROM %1 WHERE user_id = :user_id")
    .arg(tableName());

    QVariantMap params;
    params["user_id"] = userId;

    QVariantMap result = SqliteMgr::instance()->queryOne(sql, params);
    return result.value("count",   0).toInt();
}

bool HistoryDao::deleteByFileId(qint64 fileId) {
    return removeById(fileId);
}

bool HistoryDao::deleteByUserId(qint64 userId) {
    QString sql = QString("DELETE FROM %1 WHERE user_id = :user_id")
    .arg(tableName());

    QVariantMap params;
    params["user_id"] = userId;

    return SqliteMgr::instance()->execute(sql, params);
}
